package com.fkjslee;

/**
 * @author fkjslee
 * @date 2018/3/29 at 17:27
 * Copyright (c) 2018 fkjslee. All rights reserved.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

public class HttpThread implements Runnable {
    private String url;
    private byte[] param;
    private String result;
    private String threadName = "no name";

    public HttpThread(String url, String param, String threadName) {
        this.url = url;
        this.param = param.getBytes();
        this.result = "";
        this.threadName = threadName;
    }

    public HttpThread(String url, String param) {
        this.url = url;
        this.param = param.getBytes();
        this.result = "";
    }

    public static void setParamAndRun(String url, String param, String threadName) {
        HttpThread httpThread = new HttpThread(url, param, threadName);
        new Thread(httpThread).start();
    }

    public static String setParamAndRun(String url, String param, boolean needResult, String threadName) {
        HttpThread httpThread = new HttpThread(url, param, threadName);
        new Thread(httpThread).start();
        if (needResult) {
            return httpThread.getResult();
        }
        return "";
    }

    public void run() {
        OutputStream out = null;
        BufferedReader in = null;
        String temResult = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();

            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");

            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = conn.getOutputStream();
            // 发送请求参数
            out.write(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                temResult += line;
            }
        } catch (Exception e) {
            System.err.println("thread " + threadName + " error");
            System.out.println("url = " + url + "?" + new String(param));
            e.printStackTrace();
        }

        //使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        this.result = temResult;
    }

    public String getResult() {
        try {
            Integer times = 0;
            while ((result == null || result.length() == 0) && (times++ < 30)) {
                Thread.sleep(100);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return this.result;
    }
}
